//
//  NFSRadioViewController.m
//  NeedForSpeed
//
//  Created by 王国强 on 15/10/19.
//  Copyright © 2015年 Avenger. All rights reserved.
//

#import "NFSRadioViewController.h"

#import "BaseHeader.h"

#import "NFSRadioView.h"

#import "NFSMoreRadioViewController.h"

#import "NFSDirectModel.h"

#import "NFSRadioNextViewController.h"

#import "NFSPlayBillTableView.h"

#import "AFSoundManager.h"

#import "NFSNextModel.h"



@interface NFSRadioViewController ()<NFSRadioDelegate,playRadioDelegate>

@property(nonatomic,strong)NSMutableArray *directArray;

@property(nonatomic,strong)UIScrollView *mainScrollView;

@property(nonatomic,strong)UIView *playBillView;

@property(nonatomic,copy)NSString *adress;

@property(nonatomic,strong)UIButton *startButton;
@end

@implementation NFSRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"电台";
    
   // [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //[self createView];
    
#pragma mark 指定时间输出格式 年 月 日 (24)时 分 秒
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
#pragma mark 获取当前时间
    NSDate *date = [NSDate date];
    self.currentStr = [formatter stringFromDate:date];

    [self getDatabyWeb];
    
}
#pragma mark - 电台播放显示界面
- (void)createTabBar
{
    UIView *tabView =[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height, SCREEN_WIDTH, self.tabBarController.tabBar.frame.size.height)];
    [tabView setBackgroundColor:[UIColor blackColor]];
    [tabView setAlpha:0.8];
    [self.view addSubview:tabView];
    
    UIImageView *radioImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.tabBarController.tabBar.frame.size.height, self.tabBarController.tabBar.frame.size.height)];
    [radioImage.layer setCornerRadius:self.tabBarController.tabBar.frame.size.height / 2];
    [radioImage.layer setBorderColor:[UIColor blackColor].CGColor];
    [radioImage.layer setBorderWidth:1.];
    radioImage.layer.masksToBounds =YES;
    [radioImage setBackgroundColor:[UIColor redColor]];
    [tabView addSubview:radioImage];
    
    UILabel *carsRadioLaobel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 6, 0, SCREEN_WIDTH / 3, self.tabBarController.tabBar.frame.size.height / 2)];
    [tabView addSubview:carsRadioLaobel];
    [carsRadioLaobel setTextColor:[UIColor whiteColor]];
    [carsRadioLaobel setText:@"电台"];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 6,  self.tabBarController.tabBar.frame.size.height / 2, SCREEN_WIDTH / 3,  self.tabBarController.tabBar.frame.size.height / 2)];
    //[nameLabel setBackgroundColor:[UIColor redColor]];
    [nameLabel setFont:[UIFont systemFontOfSize:14.]];
    [nameLabel setTextColor:[UIColor grayColor]];
    [tabView addSubview:nameLabel];
#pragma mark - 电台播放
    _startButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 2 / 3 , 0, 44, 44)];
    [tabView addSubview:_startButton];
    [_startButton setImage:[UIImage imageNamed:@"home_video_play@3x.png"] forState:UIControlStateNormal];
    [_startButton setImage:[UIImage imageNamed:@"home_video_stop@3x.png"] forState:UIControlStateSelected];
    [_startButton addTarget:self action:@selector(offOrNo:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *moreVideo = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreVideo setFrame:CGRectMake(SCREEN_WIDTH *5 /6, 0, 44, 44)];
    [moreVideo  setImage:[UIImage imageNamed:@"iconfont-gengduo-2.png"] forState:UIControlStateNormal];
    [tabView addSubview:moreVideo];
    [moreVideo addTarget:self action:@selector(VideosButton) forControlEvents:UIControlEventTouchUpInside];
    
    for (NFSDirectModel *model in _directArray) {
        
        if ([self.currentStr compare:model.starttime] >= 0 &&  [self.currentStr compare:model.endtime] <= 0) {
            NSURL *url = [NSURL URLWithString:model.radiopicurl];
            [radioImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hoder.jpg"]];
            [nameLabel setText:model.album];
        }
    }
    
    
}
- (void)offOrNo:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        [self getRodioStart];
    }
    else
    {
        [[AFSoundManager sharedManager]stop];
    }
    
}
- (void)getRodioStart
{
#pragma mark - 根据当前时间判断播放的内容
    for (NFSDirectModel *model in _directArray) {
        if ([self.currentStr compare:model.starttime] >= 0 &&  [self.currentStr compare:model.endtime] <= 0) {
            _adress = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/News/newsonewradiolist-pm1-aid%@-p1-s50-tp0.json",model.albumid];

            [self getdata];
            break;
        }
    }

  
}
- (void)getdata
{
    [AFNETWORK getdataWithString:_adress block:^(id block) {
        for (NSDictionary *dic in [[block objectForKey:@"result"]objectForKey:@"list"]) {
            NFSNextModel *model = [[NFSNextModel alloc]initWithDictionary:dic];
            //NSLog(@"%@",model.program_live_time);
            if ([model.program_live_time isEqual:@"20151023期"]) {
                [self startRadio:model.program_fileurl];
                break;
            }
            else
            {
            }
        }
    }];
}
#pragma mark - 播放的代理
- (void)playRadio:(NSString *)map3
{
    [_startButton setSelected:YES];
    [self  startRadio:map3];
}
- (void)startRadio:(NSString *)mp3
{
    [[AFSoundManager sharedManager]startStreamingRemoteAudioFromURL:mp3 andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        
        if (error) {
            NSLog(@"error");
        }
        
    }];

}
- (void)VideosButton
{
    [_playBillView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
}
//解析数据
- (void)getDatabyWeb
{
    _directArray = [[NSMutableArray alloc]init];
    [AFNETWORK getdataWithString:@"http://app.api.autohome.com.cn/autov5.0.0/news/radio-pm1.json" block:^(id block) {
        for (NSDictionary *dic in [[block objectForKey:@"result"]objectForKey:@"list"]) {
            NFSDirectModel *model = [[NFSDirectModel alloc]initWithDictionary:dic];
            [_directArray addObject:model];
        }
        //NSLog(@"%lu",(unsigned long)_directArray.count);
        [self createView];
        [self createTabBar];
        [self createPlayBill];
    }];
}
- (void)createView
{

    _mainScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT  )];
    [_mainScrollView setContentSize:CGSizeMake(0, SCREEN_HEIGHT * 6 / 5 + self.tabBarController.tabBar.frame.size.height)];
    [_mainScrollView setBounces:NO];
    [self.view addSubview:_mainScrollView];
    
    UIImageView *radioView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT *2 / 5)];
    [radioView setImage:[UIImage imageNamed:@"Radioback.jpg"]];

    [radioView setBackgroundColor:[UIColor blackColor]];
    [_mainScrollView addSubview:radioView];
    //直播名字
    UILabel *radioNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT/ 5, SCREEN_WIDTH, SCREEN_HEIGHT/ 25)];
    [radioNameLabel setTextColor:[UIColor whiteColor]];

    [radioView addSubview:radioNameLabel];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT / 5 + SCREEN_HEIGHT /25, SCREEN_WIDTH, SCREEN_HEIGHT / 25)];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [radioView addSubview:titleLabel];
    //主持人
    UILabel *anchorLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT / 5 + SCREEN_HEIGHT *2/25, SCREEN_WIDTH / 2, SCREEN_HEIGHT /25)];
    [anchorLabel setTextColor:[UIColor grayColor]];
    [radioView addSubview:anchorLabel];
    //嘉宾
    UILabel *honoredguest = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 5 + SCREEN_HEIGHT * 2 / 25, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 25)];
    [honoredguest setTextColor:[UIColor grayColor]];
    [radioView addSubview:honoredguest];
    
#pragma mark - 根据当前时间判断播放的内容
    for (NFSDirectModel *model in _directArray) {
        if ([self.currentStr compare:model.starttime] >= 0 &&  [self.currentStr compare:model.endtime] <= 0) {
            [radioNameLabel setText:model.album];
            [titleLabel setText:model.title];
            [anchorLabel setText:[NSString stringWithFormat:@"主播 : %@",model.anchor]];
            [honoredguest setText:[NSString stringWithFormat:@"%@",model.honoredguest]];

            
        }
    }
    //开始播放
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [startButton setFrame:CGRectMake(0, SCREEN_HEIGHT *2 /5 - 40, SCREEN_WIDTH/3, 40)];
    [startButton setTitle:@"收听直播" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [radioView addSubview:startButton];

    
    UILabel *myLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT * 2/ 5, SCREEN_WIDTH / 3 , SCREEN_HEIGHT / 25)];
    [myLabel setText:@"点播"];
    [myLabel setTextColor:[UIColor blackColor]];
    [_mainScrollView addSubview:myLabel];
    
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton setFrame:CGRectMake(SCREEN_WIDTH *3 / 4, SCREEN_HEIGHT *2 / 5, SCREEN_WIDTH / 4, SCREEN_HEIGHT / 25)];
    
    [moreButton setTitle:@"更多" forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(moreRadio) forControlEvents:UIControlEventTouchUpInside];
    [_mainScrollView addSubview:moreButton];
    [self createController];
    
    
}
//更多的电台
- (void)moreRadio
{
    NFSMoreRadioViewController *moreView = [[NFSMoreRadioViewController alloc]init];
    
    [self.navigationController pushViewController:moreView animated:YES];
}
- (void)createController
{
    NFSRadioView *radioCollection = [[NFSRadioView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT *11/25, SCREEN_WIDTH, SCREEN_HEIGHT  * 19 / 25) address:@"http://223.99.255.20/app.api.autohome.com.cn/autov5.0.0/News/newsmyradiolist-pm1-tp0.json" type:@"1"];
   // radioCollection.radioCollectionView.backgroundColor = [UIColor yellowColor];
    radioCollection.NFSRadioDelegate =self;
    [_mainScrollView addSubview:radioCollection];
}
//代理执行点击方法
- (void)pushNext:(NFSRadioModel *)model
{
    NFSRadioNextViewController *radioNext = [[NFSRadioNextViewController alloc]init];
    radioNext.playRadioDelegate =self;
    radioNext.adrress = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/News/newsonewradiolist-pm1-aid%@-p1-s50-tp0.json",model.album_id];
    radioNext.imageurl = model.album_bigImgurl;
    radioNext.radioName = model.album_name;
    radioNext.album_descrition = model.album_descrition;
    [self.navigationController pushViewController:radioNext animated:YES];
}
//创建节目单
- (void)createPlayBill
{
    _playBillView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT,SCREEN_WIDTH , SCREEN_HEIGHT)];
   // [_playBillView  setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:_playBillView];
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton  setFrame:CGRectMake(0,SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height, SCREEN_WIDTH, self.tabBarController.tabBar.frame.size.height)];
    [closeButton setBackgroundColor:[UIColor blackColor]];
    [closeButton setTitle:@"关闭" forState:UIControlStateNormal];
    [closeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_playBillView addSubview:closeButton];
    [closeButton addTarget:self action:@selector(closePlayBill) forControlEvents:UIControlEventTouchUpInside];
    UIView *playView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    [_playBillView addSubview:playView];
    [playView setBackgroundColor:[UIColor blackColor]];
    UILabel *playLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, 0, 80, 40)];
    [playLabel setText:@"节目单"];
    [playLabel setTextColor:[UIColor whiteColor]];
    [playView addSubview:playLabel];
    
    
    
    NFSPlayBillTableView *playTableView = [[NFSPlayBillTableView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3, SCREEN_WIDTH, SCREEN_HEIGHT *2/3 -self.tabBarController.tabBar.frame.size.height  ) Array:_directArray];
    [playTableView.playTabelView setTableHeaderView:playView];
    [_playBillView addSubview:playTableView];
}
- (void)closePlayBill
{
    [_playBillView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
